<?php

namespace Lib;

define ( 'LIB', __DIR__ );
require_once LIB . '/ExceptionExt.class.php';
/**
 *
 * @author ximi
 *        
 */
class Base {
	public $aError = array (
			'code' => 0 
	);

	/**
	 * 构造函数
	 *
	 * @param string $sConfigFile        	
	 */
	public function __construct($aParam = array()) {
		global $_CONFIG;
		// 加载配置文件
		if ($aParam ['config']) {
			$this->initConf ( $aParam ['config'] );
		} else {
			$this->initConf ();
		}
		
		// 设置时区
		$this->setTimezone ( $this->Config->get ( 'time.timezone' ) );
		
		// 初始化服务器
		$this->initServer ();
		
		// 初始化SESSION
		if ($aParam ['session']) {
			$this->initSession ( $aParam ['session'] );
		} else {
			$this->initSession ();
		}
		
		// 初始化DB
		if ($aParam ['db']) {
			$this->initDB ( $aParam ['db'] );
		}
		
		// 初始化模版引擎
		if ($aParam ['template']) {
			$this->initTemplate ( $aParam ['template'] );
		}
	}

	public function initConf($aConf = null) {
		$sFile = $aConf ['file'] ? $aConf ['file'] : ROOT . '/config.inc.php';
		$sType = $aConf ['type'] ? $aConf ['type'] : 'json';
		$this->Config->init ( $sFile, $sType );
	}

	public function initServer() {
		$this->Server->init ();
	}

	public function initSession($aConf = null) {
		$this->Session->init ( $aConf );
	}

	public function initDB($aConf) {
		$this->DB->init ( $aConf );
	}

	public function initTemplate($aConf) {
		global $_CONFIG;
		if (is_array ( $aConf )) {
			$this->Template ( $aConf );
		} else {
			$this->Template ( $_CONFIG ['template'] );
		}
	}

	/**
	 * 获取方法
	 *
	 * @param string $sClassName        	
	 * @return object boolean
	 */
	public function __get($sClassName) {
		$aType = array (
				'Lib',
				'Model',
				'Action' 
		);
		if (in_array ( $sClassName, $aType )) {
			require_once __DIR__ . '/' . $sClassName . '.class.php';
			$sClass = __NAMESPACE__ . '\\' . $sClassName;
			$oObj = new $sClass ();
			$this->__set ( $sClassName, $oObj );
			return $oObj;
		} else {
			foreach ( $aType as $sType ) {
				if (defined ( strtoupper ( $sType ) )) {
					$sFile = constant ( strtoupper ( $sType ) ) . '/' . $sClassName . '.class.php';
					$sDir = constant ( strtoupper ( $sType ) ) . '/' . $sClassName;
					if (is_file ( $sFile ) || is_dir ( $sDir )) {
						return $this->$sType->$sClassName;
					}
				}
			}
		}
		return false;
	}

	/**
	 * 检查类是否存在
	 *
	 * @param 类名 $sClassName        	
	 * @param 类型active、model、lib（默认） $sType        	
	 * @return boolean
	 */
	public function checkClassExist($sClassName, $sType = 'lib') {
		$sType = strtolower ( $sType );
		$sClassName = ucfirst ( $sClassName );
		if ($sType == 'action') {
			if (! defined ( 'ACTION' )) {
				$sActionPath = $this->Config->get ( 'action.path' );
				if (! $sActionPath) {
					$sActionPath = ROOT . '/action';
				} else {
					$sActionPath = ROOT . '/' . $sActionPath;
				}
				define ( 'ACTION', $sActionPath );
			}
			$sFile = ACTION . '/' . $sClassName . '.class.php';
			if (is_file ( $sFile )) {
				return true;
			} else {
				return false;
			}
		} elseif ($sType == 'model') {
			if (! defined ( 'MODEL' )) {
				$sModelPath = $this->Config->get ( 'model.path' );
				if (! $sModelPath) {
					$sModelPath = ROOT . '/model';
				} else {
					$sModelPath = ROOT . '/' . $sModelPath;
				}
				define ( 'MODEL', $sModelPath );
			}
			$sFile = MODEL . '/' . $sClassName . '.class.php';
			if (is_file ( $sFile )) {
				return true;
			} else {
				return false;
			}
		} elseif (is_file ( __DIR__ . '/' . $sClassName . '.class.php' )) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 将方法保存到属性
	 *
	 * @param string $sName        	
	 * @param object $sValue        	
	 */
	public function __set($sName, $oValue) {
		$this->$sName = $oValue;
	}

	public function __call($sClassName, $aParam = array()) {
		$oObj = $this->__get ( $sClassName );
		if ($aParam) {
			call_user_func_array ( array (
					$oObj,
					'__construct' 
			), $aParam );
		}
	}

	public function setTimezone($sTimezone = 'PRC') {
		$sTimezone = $sTimezone ? $sTimezone : 'PRC';
		date_default_timezone_set ( $sTimezone );
	}

	public function setError($iCode, $sMessage = null) {
		$this->aError = array (
				'code' => $iCode 
		);
		if ($iCode > 0 && $sMessage) {
			$this->aError ['message'] = $sMessage;
		}
		/*
		 * echo '<pre>'; $aDebug = debug_backtrace (); var_dump ( $aDebug [1] );
		 * $sClass = strtoupper ( str_replace ( '\\', '_', $aDebug [1] ['class']
		 * ) );
		 */
	}
}

function o() {
	global $o;
	if (! $o) {
		$o = new Base ();
	}
	return $o;
}

function m() {
	global $o;
	if (! $o) {
		return false;
	}
	return $o->Model;
}

function a() {
	global $o;
	if (! $o) {
		return false;
	}
	return $o->Action;
}